﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorldConqueror.Libraries.Maths.MathShapes;
using WorldConqueror.Libraries.Maths.ExtensionMethods;

namespace WorldConqueror.Libraries.Maths.Functions.Bearing
{
    public interface IBearingBetweenCoordinates : ITwoParamsFunction<IMathCoordinate, IMathCoordinate, double>
    {
    }

    public class BearingBetweenCoordinates : IBearingBetweenCoordinates
    {
        public BearingBetweenCoordinates(IMathCoordinate parameter1, IMathCoordinate parameter2)
        {
            Parameter1 = parameter1;
            Parameter2 = parameter2;
        }

        public IMathCoordinate Parameter1
        {
            get;
            private set;
        }

        public IMathCoordinate Parameter2
        {
            get;
            private set;
        }

        public double Calculate()
        {
            var longitudeDifference = Parameter2.Longitude.ToRadians() - Parameter1.Longitude.ToRadians();
            var latitude1InRadians = Parameter1.Latitude.ToRadians();
            var latitude2InRadians = Parameter2.Latitude.ToRadians();


            var y = Math.Sin(longitudeDifference) * Math.Cos(latitude2InRadians);
            var x = Math.Cos(latitude1InRadians) * Math.Sin(latitude2InRadians) - Math.Sin(latitude1InRadians)
                * Math.Cos(latitude2InRadians) * Math.Cos(longitudeDifference);

            var bearing = Math.Atan2(y, x);

            return bearing;
        }
    }
}
